package com.cyss.greenball.world;

import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;

import com.cyss.greenball.exception.GameOverException;
import com.cyss.greenball.util.SystemUtil;

import android.graphics.Canvas;
import android.graphics.Color;

public class RectangleBarrier extends ElementObject {
	private float left, top, right, bottom;

	public RectangleBarrier(BallWorld world, float left, float top,
			float right, float bottom) {
		this(world, left, top, right, bottom, Color.RED);
	}

	public RectangleBarrier(BallWorld world, float left, float top,
			float right, float bottom, int color) {
		super(world);
		this.left = left;
		this.top = top;
		this.right = right;
		this.bottom = bottom;
		paint.setColor(color);
		instance();
	}

	@Override
	public void instance() {
		PolygonDef shape = new PolygonDef();
		shape.setAsBox((right - left) / 2 / SystemUtil.RATE, (bottom - top) / 2
				/ SystemUtil.RATE);
		shape.density = 0;
		shape.friction = 0.8f;
		shape.restitution = 0.3f;
		BodyDef bodyDef = new BodyDef();
		bodyDef.position.set((right + left) / 2 / SystemUtil.RATE,
				((bottom + top) / 2) / SystemUtil.RATE);
		Body body = world.createBody(bodyDef);
		body.createShape(shape);
	}

	@Override
	public void onDraw(Canvas canvas) throws GameOverException {
		canvas.drawRect(left, top, right, bottom, paint);
	}

}
